// pages/orderCenter/lost/detail.js
const app = getApp();
var topOpacity = 0;
var ss = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "失物招领", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    nowTime: new Date().getTime(), //获取当前日期
    endTime: 1711911820000, //结束日期时间戳
    remainTime: null,
    countDownTxt: null,
    phone: '02381395565',
    wxNo: 'LYJC-81395565',

    shareTitle: '',
    shareImg: '',
    id: 0,
    goodsInfo: []
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  tel() {
    let that = this;
    let phone = that.data.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
  //复制
  copyBtn(e) {
    let that = this;
    let wxNo = that.data.wxNo;
    wx.setClipboardData({
      data: wxNo,
      success: function(res) {
        wx.showToast({
          title: '复制成功',
        })
      },
      fail: function(res) {
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    that.setData({
      remainTime: that.data.endTime - that.data.nowTime
    });

    var id = options.id;
    if (!id) {
      that.showTip('抱歉,未获取到相应的失物id哦');
      setTimeout(function() {
        wx.redirectTo({
          url: '/pages/orderCenter/lost/index'
        });
        return;
      }, 2000);
    } else {
      that.setData({
        id: id
      });
    }

    //获取失物信息
    that.getLostDetailPageInfo(id);


    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getLostDetailPageInfo(id) {
    var that=this;
    wx.showLoading({
      title: '数据获取中...'
    });

    wx.request({
      url: that.data.baseUrl + 'getLostDetailPageInfo.php',
      method: 'POST',
      data: {
        'id': id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          var goodsInfo = res.data.goodsInfo;
          goodsInfo['end_time'] = that.timefun(goodsInfo['end_time']);
          that.setData({
            goodsInfo: goodsInfo
          });
        } else {
          that.showTip(res.data.msg);
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/orderCenter/lost/index'
            });
            return;
          }, 2000);
        }
      },
      fail: function (res) {
        wx.hideLoading();
        setTimeout(function () {
          wx.redirectTo({
            url: '/pages/orderCenter/lost/index'
          });
          return;
        }, 2000);
      }
    });


  },

  // 传时间就行
  timefun(str) {
    let date = new Date();
    let now = date.getTime();
    //设置截止时间
    // let str = "2021/11/15 00:00:00";
    let endDate = new Date(str);
    let end = endDate.getTime();
    let leftTime = end - now;
    let d, h, m, s;
    if (leftTime >= 0) {
      d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
      h = Math.floor((leftTime / 1000 / 60 / 60) % 24);
      m = Math.floor((leftTime / 1000 / 60) % 60);
      s = Math.floor((leftTime / 1000) % 60);
    } else {
      clearInterval(this.timer);
    }
    return `${d}天${h}时${m}分`;
  },


  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo_order.php',
      method: 'POST',
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function(res) {}
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    ss.countDown(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/orderCenter/lost/detail?id=' + that.data.id,
      imageUrl: that.data.shareImg,
      success: function(res) {

      },
      fail: function(res) {

      }
    }
  }
})